package com.example.animactivity;

import android.os.Bundle;

import androidx.core.view.ViewCompat;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.bottomsheet.BottomSheetDialog;

public class DetialFragment extends Fragment implements View.OnClickListener {
    private static final String ARG_PARAM1 = "param1";

    private String mParam1;
    private ImageView detial_img;
    private FrameLayout container;
    private LinearLayout detial_ll;

    public static DetialFragment newInstance(String param1) {
        DetialFragment fragment = new DetialFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_detial, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        TextView detial_tv = view.findViewById(R.id.detial_tv);
        detial_img = view.findViewById(R.id.detial_img);
        container = view.findViewById(R.id.container);
        detial_ll = view.findViewById(R.id.detial_ll);
        detial_tv.setText(mParam1);
        ViewCompat.setTransitionName(detial_img, "shared element");
        detial_tv.setOnClickListener(this);
        detial_ll.setVisibility(View.VISIBLE);
        container.setVisibility(View.GONE);

    }

    @Override
    public void onClick(View v) {
//        dialog();
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .addSharedElement(detial_img, "shared element")
                .replace(R.id.container, ScalFragment.newInstance())
                .addToBackStack(null)
                .commit();
        detial_ll.setVisibility(View.GONE);
        container.setVisibility(View.VISIBLE);
    }


    private void dialog() {
        // 参数2：设置BottomSheetDialog的主题样式；将背景设置为transparent，这样我们写的shape_bottom_sheet_dialog.xml才会起作用
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(getContext(), R.style.BottomSheetDialog);
// 底部弹出的布局
        View bottomView = LayoutInflater.from(requireContext()).inflate(R.layout.bottom_sheet_layout, null);
        bottomSheetDialog.setContentView(bottomView);
//设置点击dialog外部不消失
//bottomSheetDialog.setCanceledOnTouchOutside(false);
        bottomSheetDialog.show();


    }
}